using System;
using LoD.WPClient.Modules;
using LoD.WPClientLibrary.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace LoD.WPClient {
    public class GameClass : Game {
        private IGameModule[] _modules;
        private TouchLocation _oldTouch;
        private Random _random;
        private GameSettings _settings;
        private Song _backgroundMusic;

        public GameClass() {
            this.Graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            TargetElapsedTime = TimeSpan.FromTicks(333333);
            InactiveSleepTime = TimeSpan.FromSeconds(1);
            this._oldTouch = new TouchLocation();
            this._random = new Random();
            this._settings = new GameSettings();
            this.GameState = GameStates.Title;
            this.OldState = this.GameState;
        }

        public GraphicsDeviceManager Graphics { get; private set; }
        public SpriteBatch SpriteBatch { get; private set; }
        public GameStates GameState { get; set; }
        public GameStates OldState { get; set; }

        protected override void Initialize() { base.Initialize(); }

        protected override void LoadContent() {
            this.SpriteBatch = new SpriteBatch(GraphicsDevice);
            this._backgroundMusic = Content.Load<Song>(@"Music/HeroicDemise");

            this._modules = new IGameModule[2];
            this._modules[0] = new TitleScreen(this);
            this._modules[1] = new Playing(this);

            foreach (IGameModule mod in this._modules) mod.LoadContent(Content);
        }

        protected override void UnloadContent() { }

        protected override void Update(GameTime gameTime) {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) {
                switch (this.GameState) {
                    case GameStates.Playing:
                        this.GameState = GameStates.Title;
                        break;
                    case GameStates.Title:
                        Exit();
                        break;
                    default:
                        this.GameState = GameStates.Title;
                        break;
                }
            }

            if (MediaPlayer.State != MediaState.Playing && MediaPlayer.GameHasControl)
                MediaPlayer.Play(this._backgroundMusic);

            TouchCollection touchInput = TouchPanel.GetState();
            var touch = new TouchLocation();

            if (touchInput.Count > 0) {
                touch = touchInput[0];
                this._oldTouch = touch;
            }

            if (this.GameState != this.OldState) {
                this.OldState = this.GameState;
                this._modules[(int)this.GameState].Reset();
            }

            this._modules[(int)this.GameState].Update(touch, gameTime);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime) {
            GraphicsDevice.Clear(Color.Black);
            this.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            this._modules[(int)this.GameState].Draw(gameTime);
            this.SpriteBatch.End();
            base.Draw(gameTime);
        }

        #region Nested type: GameStates

        public enum GameStates
        {
            Title = 0,
            Playing = 1,
            Options = 2,
            Credits = 3,
            ServerList = 4,
            ExitCredits = 5
        }

        #endregion

        #region Nested type: Orientation

        internal enum Orientation {
            E = 0,
            N = 1,
            NE = 2,
            NW = 3,
            S = 4,
            SE = 5,
            SW = 6,
            W = 7
        }

        #endregion
    }
}